home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Human Interface Toolbox / Fragment Tool / AppleEventStuff.c next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  4.4 KB  |  186 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        AppleEventStuff.c
  3.  
  4.     Contains:    Handlers for the 4 "required" events
  5.  
  6.     Written by: Chris White    
  7.  
  8.     Copyright:    Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 8/5/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. #ifndef __MIXEDMODE__
  25.     #include <MixedMode.h>
  26. #endif
  27.  
  28. #ifndef __GESTALT__
  29.     #include <Gestalt.h>
  30. #endif
  31.  
  32. #ifndef __APPLEEVENTS__
  33.     #include <AppleEvents.h>
  34. #endif
  35.  
  36.  
  37.  
  38.  
  39.  
  40. #ifndef __FRAGMENTTOOL__
  41.     #include "FragmentTool.h"
  42. #endif
  43.  
  44. #ifndef __PROTOTYPES__
  45.     #include "Prototypes.h"
  46. #endif
  47.  
  48. #ifndef __APPLEEVENTSTUFF__
  49.     #include "AppleEventStuff.h"
  50. #endif
  51.  
  52.  
  53.  
  54.  
  55.  
  56. //
  57. // Prototypes for those routines which are private to this source file
  58. //
  59.  
  60. static pascal OSErr HandleOapp ( AEDescList* aevt, AEDescList* reply, long refCon );
  61. static pascal OSErr HandleQuit ( AEDescList* aevt, AEDescList* reply, long refCon );
  62. static pascal OSErr HandleOdoc ( AEDescList* aevt, AEDescList* reply, long refCon );
  63. static pascal OSErr HandlePdoc ( AEDescList* aevt, AEDescList* reply, long refCon );
  64.  
  65.  
  66.  
  67.  
  68. void InstallAppleEventHandlers ( void )
  69. {
  70.     OSErr    theErr;
  71.     int32    theResult;
  72.  
  73.     theErr = Gestalt ( gestaltAppleEventsAttr, &theResult );
  74.     if ( theErr == noErr )
  75.     {    
  76.         AEInstallEventHandler ( kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc ( HandleOapp ), 0, false );
  77.         AEInstallEventHandler ( kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerProc ( HandleOdoc ), 0, false );
  78.         AEInstallEventHandler ( kCoreEventClass, kAEPrintDocuments, NewAEEventHandlerProc ( HandlePdoc ), 0, false );
  79.         AEInstallEventHandler ( kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc ( HandleQuit ), 0, false );
  80.     }
  81.     
  82.     return;
  83.     
  84. }    // InstallAppleEventHandlers
  85.  
  86.  
  87. //
  88. // When we get an "oapp" event, we've been launched without any
  89. // document to open. So, we'll create an untitled document.
  90. //
  91. pascal OSErr HandleOapp ( AEDescList* aevt, AEDescList* reply, long refCon )
  92. {
  93.     #pragma unused(aevt,reply,refCon)
  94.     DoNewDocument ( );
  95.     
  96.     return noErr;
  97. }
  98.  
  99.  
  100.  
  101. //
  102. // The "odoc" and "pdoc" messages contain a list of aliases as the direct paramater.
  103. // This means that we'll need to extract the list, count the list's elements, and
  104. // then process each file in turn.
  105. //                                                                                                 */
  106. pascal OSErr HandleOdoc ( AEDescList* aevt, AEDescList* reply, long refCon )
  107. {
  108.     #pragma unused(reply,refCon)
  109.     AEDesc        fileListDesc = {'NULL', NULL};
  110.     long        numFiles;
  111.     DescType    actualType;
  112.     long        actualSize;
  113.     AEKeyword    actualKeyword;
  114.     FSSpec        oneFile;
  115.     long        index;
  116.     OSErr        err;
  117.                         
  118.     
  119.     // Extract the list of aliases into fileListDesc
  120.     err = AEGetKeyDesc( aevt, keyDirectObject, typeAEList, &fileListDesc );
  121.     if ( err ) goto CleanupAndBail;
  122.         
  123.     // Count the list elements
  124.     err = AECountItems( &fileListDesc, &numFiles);
  125.     if ( err ) goto CleanupAndBail;
  126.     
  127.     // Now get each file from the list and process it.
  128.     // Even though the event contains a list of alises, the Apple Event Manager
  129.     // will convert each alias to an FSSpec if we ask it to.
  130.     for ( index = 1; index <= numFiles; index ++ )
  131.     {
  132.         err = AEGetNthPtr ( &fileListDesc, index, typeFSS, &actualKeyword,
  133.                             &actualType, (Ptr) &oneFile, sizeof ( oneFile ), &actualSize );
  134.                             
  135.         #if DEBUGGING
  136.         if ( err )
  137.             DebugStrNum ( "\p AEGetNthPtr returned ", err );
  138.         #endif
  139.         
  140.         if ( err )
  141.             break;
  142.         
  143.         err = OpenDocument ( &oneFile, nil );
  144.         
  145.         #if DEBUGGING
  146.         if ( err )
  147.             DebugStrNum ( "\p OpenDocument returned ", err );
  148.         #endif
  149.         
  150.         if ( err )
  151.             break;
  152.     }
  153.     
  154.     
  155.     // Since we want to free all the storage this routine allocates,
  156.     // we'll continue execution here.
  157.     
  158. CleanupAndBail:
  159.     
  160.     AEDisposeDesc ( &fileListDesc );
  161.     return err;
  162.     
  163. } // HandleOdoc
  164.  
  165.  
  166.  
  167. pascal OSErr HandlePdoc ( AEDescList* aevt, AEDescList* reply, long refCon )
  168. {
  169.     #pragma unused(aevt,reply,refCon)
  170.     return errAEEventNotHandled;
  171. }
  172.  
  173.  
  174.  
  175. pascal OSErr HandleQuit ( AEDescList* aevt, AEDescList* reply, long refCon )
  176. {
  177.     #pragma unused(aevt,reply,refCon)
  178.     gQuit = true;
  179.     
  180.     return noErr;
  181. }
  182.  
  183.  
  184.  
  185.  
  186.